package cn.com.customer.manager.ui.task.addTask

import android.app.Activity
import android.app.DatePickerDialog
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.DatePicker
import cn.com.customer.manager.R
import cn.com.customer.manager.adapter.SimpleAdapter
import cn.com.customer.manager.base.BaseActivity
import cn.com.customer.manager.bean.*
import cn.com.customer.manager.ui.search.SearchActivity
import com.xuexiang.xui.utils.ResUtils
import com.xuexiang.xui.utils.WidgetUtils
import com.xuexiang.xui.widget.spinner.editspinner.EditSpinnerAdapter
import com.xuexiang.xui.widget.spinner.materialspinner.MaterialSpinner
import com.xuexiang.xui.widget.spinner.materialspinner.MaterialSpinner.OnNothingSelectedListener
import com.xuexiang.xutil.data.DateUtils
import com.xuexiang.xutil.tip.ToastUtils
import kotlinx.android.synthetic.main.activity_add_task.*
import java.util.*


class AddTaskActivity : BaseActivity(), AddTaslContract.View, DatePickerDialog.OnDateSetListener {
    lateinit var nowType: Type
    lateinit var nowState: StateInfo
    lateinit var nowMarkWay: Type
    var nowTime: String = DateUtils.getNowString(DateUtils.yyyyMMddHHmmss.get())
    //    private lateinit var customerId: String
//    private lateinit var customerName: String
    private var isRemind: String = "1"
    private var customerIds: MutableList<Customer> = mutableListOf()
    private var customerNames: MutableList<String> = mutableListOf()
    private lateinit var mAdapter: SimpleAdapter
    private lateinit var nowUser: UserList
    private lateinit var userDate: List<UserList>

    var mPresenter: AddTaslContract.Presenter = AddTaskPresenter(this)
    override fun getLayoutId(): Int {
        return R.layout.activity_add_task//To change body of created functions use File | Settings | File Templates.
    }

    override fun initView(savedInstanceState: Bundle?) {
        initActionBar(getString(R.string.add_task))//To change body of created functions use File | Settings | File Templates.
//        customerId = intent.getStringExtra("customerId")
//        customerName = intent.getStringExtra("name")
        add_task_switch.setOnCheckedChangeListener { compoundButton, b ->
            if (b) {
                isRemind = "1"
                ll_tip.visibility = View.VISIBLE
            } else {
                isRemind = "2"
                ll_tip.visibility = View.GONE
            }
        }
        button_add_task.setOnClickListener {
            send()
        }
        add_task_customer.setOnClickListener {
            startActivityForResult(Intent(this, SearchActivity::class.java), 1)
        }
        WidgetUtils.initGridRecyclerView(add_task_customer_rv, 3)
        mAdapter = SimpleAdapter(R.layout.item_simple_text_1)
        mAdapter.setNewData(customerNames)
        add_task_customer_rv.adapter = mAdapter
        add_task_time.setOnClickListener {
            showDateDialog()
        }
    }

    override fun setMarkType(markTypes: RemindWayResult) {
        var remindWayList = mutableListOf<String>()
        if (markTypes.remindWayList.isNotEmpty()) {
            nowMarkWay = markTypes.remindWayList[0]
            markTypes.remindWayList.forEach {
                remindWayList.add(it.name)
            }
        }
        add_task_remind_way.setItems(remindWayList)
        add_task_remind_way.setOnItemSelectedListener(MaterialSpinner.OnItemSelectedListener<Any?> { spinner, position, id, item ->
            nowMarkWay = markTypes.remindWayList[position]
        })
        add_task_remind_way.setOnNothingSelectedListener(OnNothingSelectedListener { spinner -> })
    }

    override fun addSuccess() {
        ToastUtils.toast(getString(R.string.add_success))
        finish()
    }

    fun send() {
        var params = TaskAddInfo(customerIds, nowUser.id, isRemind, add_task_remark.text.toString(), nowMarkWay.code, nowTime, nowType.code)
        mPresenter.addTask(params)
    }

    override fun setTaskType(taskTypes: TypeResult) {
        var typeList = mutableListOf<String>()
        if (taskTypes.typeList.isNotEmpty()) {
            nowType = taskTypes.typeList[0]
            taskTypes.typeList.forEach {
                typeList.add(it.name)
            }
        }
        add_task_type.setItems(typeList)
        add_task_type.setOnItemSelectedListener(MaterialSpinner.OnItemSelectedListener<Any?> { spinner, position, id, item ->
            nowType = taskTypes.typeList[position]
        })
        add_task_type.setOnNothingSelectedListener(OnNothingSelectedListener { spinner -> })
    }

    override fun setUser(userList: MutableList<UserList>) {
        var userNames: MutableList<String> = mutableListOf()
        nowUser = userList[0]
        this.userDate = userList
        userList.forEach {
            userNames.add(it.name)
        }
        var editSpinnerAdapter = EditSpinnerAdapter(this, userNames)
                .setTextColor(ResUtils.getColor(R.color.light_green))
                .setTextSize(add_task_executor.editText.textSize)
                .setBackgroundSelector(R.drawable.selector_edit_background)
        add_task_executor.setAdapter(editSpinnerAdapter)
        add_task_executor.setOnItemClickListener(object : AdapterView.OnItemClickListener {
            override fun onItemClick(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                nowUser = userDate[p2]
            }

        })
    }

    private fun showDateDialog() {
        val calendar: Calendar = Calendar.getInstance()
        //构建一个日期对话框，该对话框已经集成了日期选择器
//DatePickerDialog的第二个构造参数指定了日期监听器
        val dialog = DatePickerDialog(this, this,
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH))
        //把日期对话框显示在界面上
        dialog.show()
    }

    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
        nowTime = String.format("%d-%d-%d 00:00:00", year, month + 1, dayOfMonth)
        add_task_time.text = nowTime
    }

    override fun setPresenter(presenter: AddTaslContract.Presenter) {
        mPresenter = presenter
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            var name = data?.getStringExtra("name")
            var id = data?.getStringExtra("id")
            if (!id.isNullOrEmpty()) {
                customerIds.add(Customer(id))
            }
            if (!name.isNullOrEmpty()) {
                customerNames.add(name)
                mAdapter.notifyDataSetChanged()
            }
        }
    }
}